package view.panel;

import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import model.AppModel;
import view.AppView;
import controller.*;

public class MenuPanel extends JPanel {
	
	private static final long serialVersionUID = -5127881458195247738L;
	
	private AppModel model;

	private JButton start;
	private JButton clearWords;
	private JButton revert;
	
	private JLabel startLabel;
	private JLabel loadLabel;
	private JLabel clearLabel;
	private JLabel modeLabel;
	
	private ArrayList<AbstractActionListener> listeners;
	private LoadWordsListener loadWordsListener;
	private StartListener startListener;
	private ClearListener clearListener;
	private RevertListener revertListener;

	public MenuPanel(AppView view)
	{
		this.setLayout(new GridLayout(4,2));
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		listeners = new ArrayList<AbstractActionListener>();
		
		start = new JButton("Start");
		startListener = new StartListener(view);
		listeners.add(startListener);
		start.addActionListener(startListener);
		start.addKeyListener(startListener);
		this.add(start);

		startLabel = new JLabel();
		startLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(startLabel);
		
		JButton loadWords = new JButton("Load");
		loadWordsListener = new LoadWordsListener(view);
		listeners.add(loadWordsListener);
		loadWords.addActionListener(loadWordsListener);
		this.add(loadWords);
		
		loadLabel = new JLabel();
		loadLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(loadLabel);
		
		clearWords = new JButton("Clear");
		clearListener = new ClearListener(view);
		listeners.add(clearListener);
		clearWords.addActionListener(clearListener);
		this.add(clearWords);
		
		clearLabel = new JLabel();
		clearLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(clearLabel);
		
		revert = new JButton("Revert");
		revertListener = new RevertListener(view);
		listeners.add(revertListener);
		revert.addActionListener(revertListener);
		this.add(revert);
		
		modeLabel = new JLabel();
		modeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		this.add(modeLabel);
	}
	
	public void update(AppModel model)
	{
		this.model = model;
		this.updateListeners();
		if(this.model.areWordsLoaded())
		{
			wordsLoaded();
		}
		else
		{
			wordsNotLoaded();
		}		
	}

	private void updateListeners()
	{
		for(AbstractActionListener listener : this.listeners)
		{
			listener.update(this.model);
		}
	}

	private void wordsLoaded()
	{
		enable3Buttons();
		int lastLoaded = model.getNumberOfLastLoadedWords();
		loadLabel.setText(lastLoaded + " "+this.getWordOrWords(lastLoaded)+" loaded. Load more?");
		clearLabel.setText("Delete all words");
		String modeText = this.model.isPolishToForeignMode() ? "Polish to Foreign" : "Foreign to Polish";
		modeLabel.setText(modeText);
		if (this.model.isFinished())
		{
			startLabel.setText("Finished. "+ this.model.getTestMarks());
			start.requestFocus();
		}
		else
		{
			int noOfWords = model.getNumberOfWords();
			startLabel.setText("Start learning with "+noOfWords+" "+this.getWordOrWords(noOfWords));
		}
	}

	private void enable3Buttons()
	{
		this.set3Buttons(true);
	}
	
	private void set3Buttons(boolean enabled)
	{
		start.setEnabled(enabled);
		clearWords.setEnabled(enabled);
		revert.setEnabled(enabled);
	}
	
	private String getWordOrWords(int number)
	{
		return number == 1 ? "word" : "words";
	}

	private void wordsNotLoaded()
	{
		disable3ButtonsClearLabels();
		loadLabel.setText("Load words");
	}
	
	private void disable3ButtonsClearLabels()
	{
		this.set3Buttons(false);
		startLabel.setText("");
		clearLabel.setText("");
		modeLabel.setText("");
	}
}